<script setup>
import { defineProps, defineEmits, ref } from 'vue';
import CommentItem from './CommentItem.vue';
import { addArticleComment } from '@/api/article';

const props = defineProps({
  comments: {
    type: Array,
    default: () => []
  },
  articleId: {
    type: [Number, String],
    required: true
  },
  isLoading: {
    type: Boolean,
    default: false
  }
});

const emit = defineEmits(['refresh-comments']);

const newComment = ref('');
const isSubmitting = ref(false);
const commentError = ref('');
const isFocused = ref(false);

const submitComment = async () => {
  // 这是评论提交的唯一入口点，只通过提交按钮触发
  if (!newComment.value.trim()) {
    commentError.value = '评论内容不能为空';
    return;
  }
  
  isSubmitting.value = true;
  commentError.value = '';
  
  try {
    // 创建评论数据对象
    const commentData = {
      articleId: props.articleId,
      content: newComment.value,
      // 这里可以从store或localStorage中获取当前用户ID
      // 实际项目中应该从用户会话或存储中获取
      userId: 1 // 默认使用1作为示例
    };
    
    // 发送评论请求
    await addArticleComment(props.articleId, commentData);
    
    // 清空评论输入框
    newComment.value = '';
    
    // 刷新评论列表
    emit('refresh-comments');
    
  } catch (error) {
    console.error('提交评论失败:', error);
    commentError.value = '提交评论失败，请稍后再试';
  } finally {
    isSubmitting.value = false;
  }
};

// 文本框焦点事件处理
const handleFocus = () => {
  isFocused.value = true;
};

const handleBlur = () => {
  isFocused.value = false;
};
</script>

<template>
  <div class="comments-container">
    <h3 class="comments-title">
      评论 ({{ comments.length }})
    </h3>
    
    <!-- 评论列表 -->
    <div v-if="isLoading" class="comments-loading">
      <div class="comments-loading-spinner"></div>
      <p>加载评论中...</p>
    </div>
    
    <div v-else-if="comments.length === 0" class="no-comments">
      暂无评论，快来抢沙发吧
    </div>
    
    <div v-else class="comments-list">
      <CommentItem 
        v-for="comment in comments" 
        :key="comment.id" 
        :comment="comment" 
      />
    </div>
    
    <!-- 发表评论 -->
    <div class="comment-form">
      <div class="comment-editor-wrapper" :class="{'is-focused': isFocused}">
        <textarea
          v-model="newComment"
          placeholder="写下你的想法..."
          class="comment-textarea"
          :disabled="isSubmitting"
          @focus="handleFocus"
          @blur="handleBlur"
          @keydown.ctrl.enter.prevent
        ></textarea>
      </div>
      
      <div v-if="commentError" class="comment-error">
        <span class="error-icon">⚠️</span>
        {{ commentError }}
      </div>
      
      <div class="comment-submit-container">
        <span class="comment-tips">请文明发言，共建和谐社区</span>
        <button 
          class="comment-submit" 
          @click="submitComment"
          :disabled="isSubmitting || !newComment.trim()"
        >
          <span v-if="isSubmitting" class="comment-spinner"></span>
          {{ isSubmitting ? '提交中...' : '发表评论' }}
        </button>
      </div>
    </div>
  </div>
</template>

<style scoped>
.comments-container {
  margin-top: 25px;
  border-top: 1px solid #eee;
  padding-top: 20px;
}

.comments-title {
  font-size: 1.1rem;
  font-weight: 600;
  color: #333;
  margin-bottom: 20px;
}

.comments-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 0;
}

.comments-loading-spinner {
  width: 30px;
  height: 30px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #00c389;
  border-radius: 50%;
  animation: spin 1s linear infinite;
  margin-bottom: 10px;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.no-comments {
  text-align: center;
  padding: 20px 0;
  color: #888;
  font-style: italic;
}

.comments-list {
  margin-bottom: 25px;
}

.comment-form {
  margin-top: 30px;
}

/* 评论输入框容器样式 */
.comment-editor-wrapper {
  display: flex;
  margin-bottom: 15px;
  transition: all 0.3s ease;
  position: relative;
  width: 100%;
}

.editor-container {
  flex-grow: 1;
  position: relative;
}

/* 普通文本框样式 */
.comment-textarea {
  width: 100%;
  min-height: 100px;
  padding: 15px;
  font-family: inherit;
  font-size: 0.95rem;
  line-height: 1.6;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  resize: vertical;
  background-color: #ffffff;
  color: #333;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.comment-textarea:focus {
  outline: none;
  border-color: #00c389;
  box-shadow: 0 4px 12px rgba(0, 195, 137, 0.1);
}

.comment-textarea::placeholder {
  color: #aaa;
  font-style: italic;
}

.comment-textarea:disabled {
  background-color: #f9f9f9;
  cursor: not-allowed;
}

/* 聚焦状态样式 */
.comment-editor-wrapper.is-focused .comment-textarea {
  border-color: #00c389;
  box-shadow: 0 4px 12px rgba(0, 195, 137, 0.1);
}

.comment-error {
  display: flex;
  align-items: center;
  color: #e74c3c;
  margin-bottom: 12px;
  font-size: 0.9rem;
  padding: 8px 12px;
  background-color: #fff8f8;
  border-radius: 6px;
  border-left: 3px solid #e74c3c;
}

.error-icon {
  margin-right: 6px;
}

.comment-submit-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.comment-tips {
  color: #888;
  font-size: 0.85rem;
  font-style: italic;
}

.comment-submit {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px 20px;
  background-color: #00c389;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 2px 8px rgba(0, 195, 137, 0.2);
}

.comment-submit:hover:not(:disabled) {
  background-color: #00a873;
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 195, 137, 0.3);
}

.comment-submit:active:not(:disabled) {
  transform: translateY(0);
  box-shadow: 0 2px 4px rgba(0, 195, 137, 0.2);
}

.comment-submit:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  background-color: #aaa;
  box-shadow: none;
}

.comment-spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid rgba(255,255,255,0.3);
  border-radius: 50%;
  border-top-color: #fff;
  animation: spin 1s ease-in-out infinite;
  margin-right: 8px;
}

/* 响应式样式 */
@media (max-width: 576px) {
  .comment-editor-wrapper {
    flex-direction: column;
  }
  
  .comment-submit-container {
    flex-direction: column;
    gap: 10px;
  }
  
  .comment-tips {
    text-align: center;
    margin-bottom: 5px;
  }
  
  .comment-submit {
    width: 100%;
  }
}
</style> 